/**
 * Created by Administrator on 2018/4/17.
 */
var express = require('express');
var app = express();
var bodyParser = require('body-parser');

app.use(bodyParser({keepExtensions: true, uploadDir: '/tmp'}));
var seasons = ['Spring', 'Summer', 'Autumn', 'Winter'];
var season = 'Spring';//当前季节
/*
 /transportservice/action/GetAllSense.do
 API 2 查询“温度”的当前值
 res {"RESULT":"S","ERRMSG":"sucess","Temp":28}
 {"RESULT":"F","ERRMSG":"fail"}
 */
app.get('/GetTemp.do', function (req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    var temp = getSeasonTemp();
    // 输出 JSON 格式
    returnData = { "RESULT": "S", "ERRMSG": "success", "Temp":temp };
    res.set("ContentType","text/json;charset=utf-8")
    res.end(JSON.stringify(returnData));
   
});

/*
 /transportservice/action/GetAllSense.do
 API 3 查询当前季节
 res {"RESULT":"S","ERRMSG":"sucess","Season":"Spring"}
 {"RESULT":"F","ERRMSG":"fail"}
 */
app.get('/GetSeason.do', function (req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    res.header("Content-Type", "text/json;charset=utf-8");
 
   
    // 输出 JSON 格式
    returnData = { "RESULT": "S", "ERRMSG": "sucess", "Season":season };
    res.end(JSON.stringify(returnData));
});


/*API 1 设置季节*/
app.post('/SetSeason.do', function (req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');

    // 输出 JSON 格式
    var requestData = { "Season": req.body.Season };
    if (!seasons.find((item) => item == requestData.Season)) {
        returnData = { "RESULT": "F", "ERRMSG": "fail" };
        res.end(JSON.stringify(returnData));
    } else {
        season = requestData.Season;
        returnData = { "RESULT": "S", "ERRMSG": "sucess" };
        res.end(JSON.stringify(returnData));
    }
});
    



/*产生指定范围内的随机数 [start,end] */
function getRandom(start, end) {
    return Number.parseInt((Math.random() * (end + 1 - start))) + start;
}


function getSeasonTemp(){
    switch (season) {
        case 'Spring':
            return getRandom(-3,20);
            
        case 'Summner':
            return getRandom(20,43);
        case 'Autumn':
            return getRandom(10,30);
        case 'Winter':
            return getRandom(-10,10);
    }
}

//开启服务器监听
var server = app.listen(8082, function () {

    var host = server.address().address
    var port = server.address().port
    console.log("应用实例，访问地址为 http://%s:%s", host, port)

})